library(lme4)
library(MuMIn)
library(multcomp)
data<-read.table(file="S1 Data.txt", header=TRUE, sep="\t")
data_c <- transform(data,cAge=Age-mean(Age),cWords=Words-mean(Words),cPropositions=Propositions-mean(Propositions),cGood=Good-mean(Good),cBad=Bad-mean(Bad),cSurvival=Survival-mean(Survival),cSocial=Social-mean(Social),cMale_Stereo=Male_Stereo-mean(Male_Stereo),cFemale_Stereo=Female_Stereo-mean(Female_Stereo),cEmotional=Emotional-mean(Emotional))
#testing H1
null<-glmer(Recall ~ (1|Set/Generation/Participant/Vignette), data = data_c, family = binomial, nAGQ = 1,control=glmerControl(optimizer="Nelder_Mead",calc.derivs = FALSE,optCtrl = list(maxfun = 100000)))
gen_only<-glmer(Recall ~ Generation + (1|Set/Generation/Participant/Vignette), data = data_c, family = binomial, nAGQ = 1,control=glmerControl(optimizer="Nelder_Mead",calc.derivs = FALSE,optCtrl = list(maxfun = 100000)))
moral<-glmer(Recall~ Morality + Generation + (1|Set/Generation/Participant/Vignette),family = binomial, nAGQ = 1, data = data_c, control=glmerControl(optimizer="Nelder_Mead",calc.derivs = FALSE, optCtrl = list(maxfun = 100000)))
anova(moral,gen_only)
#testing H2 and H3 - fitting models
m01<-glmer(Recall ~ cAge + Gender + cWords + cPropositions + cGood + cBad + cSurvival + cSocial + cMale_Stereo + cFemale_Stereo + Emotion + cEmotional + Generation + (1|Set/Generation/Participant/Vignette), data = data_c, family = binomial, nAGQ = 1,control=glmerControl(optimizer="Nelder_Mead",calc.derivs = FALSE,optCtrl = list(maxfun = 100000)))
m02<-glmer(Recall ~ cAge + Gender + cWords + cGood + cBad + cSurvival + cSocial + cMale_Stereo + cFemale_Stereo + Emotion + cEmotional + Generation + (1|Set/Generation/Participant/Vignette), data = data_c, family = binomial, nAGQ = 1,control=glmerControl(optimizer="Nelder_Mead",calc.derivs = FALSE,optCtrl = list(maxfun = 100000)))
m03<-glmer(Recall ~ cAge + Gender + cWords + cGood + cBad + cSurvival + cSocial + cMale_Stereo + cFemale_Stereo + Emotion + Generation + (1|Set/Generation/Participant/Vignette), data = data_c, family = binomial, nAGQ = 1,control=glmerControl(optimizer="Nelder_Mead",calc.derivs = FALSE,optCtrl = list(maxfun = 100000)))
m04<-glmer(Recall ~ cAge + Gender + cWords + cGood + cBad + cSocial + cMale_Stereo + cFemale_Stereo + Emotion + Generation + (1|Set/Generation/Participant/Vignette), data = data_c, family = binomial, nAGQ = 1,control=glmerControl(optimizer="Nelder_Mead",calc.derivs = FALSE,optCtrl = list(maxfun = 100000)))
m05<-glmer(Recall ~ cAge + Gender + cWords + cGood + cBad + cMale_Stereo + cFemale_Stereo + Emotion + Generation + (1|Set/Generation/Participant/Vignette), data = data_c, family = binomial, nAGQ = 1,control=glmerControl(optimizer="Nelder_Mead",calc.derivs = FALSE,optCtrl = list(maxfun = 100000)))
m06<-glmer(Recall ~ cAge + Gender + cWords + cGood + cMale_Stereo + cFemale_Stereo + Emotion + Generation + (1|Set/Generation/Participant/Vignette), data = data_c, family = binomial, nAGQ = 1,control=glmerControl(optimizer="Nelder_Mead",calc.derivs = FALSE,optCtrl = list(maxfun = 100000)))
m07<-glmer(Recall ~ cAge + Gender + cWords + cGood + cMale_Stereo + Emotion + Generation + (1|Set/Generation/Participant/Vignette), data = data_c, family = binomial, nAGQ = 1,control=glmerControl(optimizer="Nelder_Mead",calc.derivs = FALSE,optCtrl = list(maxfun = 100000)))
m08<-glmer(Recall ~ cAge + Gender + cWords + cGood + Emotion + Generation + (1|Set/Generation/Participant/Vignette), data = data_c, family = binomial, nAGQ = 1,control=glmerControl(optimizer="Nelder_Mead",calc.derivs = FALSE,optCtrl = list(maxfun = 100000)))
m09<-glmer(Recall ~ cAge + Gender + cWords + cMale_Stereo + Emotion + Generation + (1|Set/Generation/Participant/Vignette), data = data_c, family = binomial, nAGQ = 1,control=glmerControl(optimizer="Nelder_Mead",calc.derivs = FALSE,optCtrl = list(maxfun = 100000)))
m10<-glmer(Recall ~ cAge + Gender + cWords + cGood + cMale_Stereo + Generation + (1|Set/Generation/Participant/Vignette), data = data_c, family = binomial, nAGQ = 1,control=glmerControl(optimizer="Nelder_Mead",calc.derivs = FALSE,optCtrl = list(maxfun = 100000)))
m11<-glmer(Recall ~ cAge + Gender + cGood + cMale_Stereo + Emotion + Generation + (1|Set/Generation/Participant/Vignette), data = data_c, family = binomial, nAGQ = 1,control=glmerControl(optimizer="Nelder_Mead",calc.derivs = FALSE,optCtrl = list(maxfun = 100000)))
m12<-glmer(Recall ~ cAge + cWords + cGood + cMale_Stereo + Emotion + Generation + (1|Set/Generation/Participant/Vignette), data = data_c, family = binomial, nAGQ = 1,control=glmerControl(optimizer="Nelder_Mead",calc.derivs = FALSE,optCtrl = list(maxfun = 100000)))
m13<-glmer(Recall ~ Gender + cWords + cGood + cMale_Stereo + Emotion + Generation + (1|Set/Generation/Participant/Vignette), data = data_c, family = binomial, nAGQ = 1,control=glmerControl(optimizer="Nelder_Mead",calc.derivs = FALSE,optCtrl = list(maxfun = 100000)))
#model selection table
model.sel(m01,m02,m03,m04,m05,m06,m07,m08,m09,m10,m11,m12,m13)
#model averaging 
average_model<-model.avg(m07,m08,m06)
#emotion comparison using best model (m07)
m07_mcp<-glht(m07,linfct=mcp(Emotion="Tukey"))
#testing for recall of moral propositions
gen_onlyMP<-glmer(Moral_Prop ~ Generation + (1|Set/Generation/Participant/Vignette), data = data_c, family = binomial, nAGQ = 1,control=glmerControl(optimizer="Nelder_Mead",calc.derivs = FALSE,optCtrl = list(maxfun = 100000)))
moralMP<-glmer(Moral_Prop~ Morality + Generation + (1|Set/Generation/Participant/Vignette),family = binomial, nAGQ = 1, data = data_c, control=glmerControl(optimizer="Nelder_Mead",calc.derivs = FALSE, optCtrl = list(maxfun = 100000)))
anova(moralMP,gen_onlyMP)
goodMP<-glmer(Moral_Prop~ cGood + Generation + (1|Set/Generation/Participant/Vignette),family = binomial, nAGQ = 1, data = data_c, control=glmerControl(optimizer="Nelder_Mead",calc.derivs = FALSE, optCtrl = list(maxfun = 100000)))
badMP<-glmer(Moral_Prop~ cBad + Generation + (1|Set/Generation/Participant/Vignette),family = binomial, nAGQ = 1, data = data_c, control=glmerControl(optimizer="Nelder_Mead",calc.derivs = FALSE, optCtrl = list(maxfun = 100000)))
anova(badMP,gen_onlyMP)
anova(badMP,goodMP)